package com.dh.web.controller;


import com.dh.core.security.RoleConstant;
import com.dh.entity.User;
import com.dh.entity.UserFlow;
import com.dh.entity.vo.UserFlowVo;
import com.dh.service.UserFlowService;
import com.dh.service.UserService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 党员流出controller
 */
@RequestMapping("/admin/userFlow")
@Controller
public class UserFlowController {

    @Autowired
    private UserFlowService flowService;

    @Autowired
    private UserService userService;

    /**
     * 转到列表展示页
     *
     * @param model
     * @param page
     * @param nickname
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @RequiresUser
    public String show(Model model, @RequestParam(required = false, value = "page", defaultValue = "1") int page, String nickname) {

        Page<UserFlowVo> pagination =
            flowService.page(new PageRequest(page - 1, 100, Sort.Direction.DESC, "createTime"));
        model.addAttribute("pagination", pagination);
        return "admin/user-flow/flow_list";
    }

    /**
     * 转到添加页面
     *
     * @return
     */
    @RequestMapping("/new")
    @RequiresUser
    public String editNew(Model model) {
        List<User> users = userService.findAllDy();
        model.addAttribute("dys", users);
        return "admin/user-flow/flow_create";
    }



    /**
     * 添加党员流出信息
     *
     * @param flow
     * @param redirectAttributes
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @RequiresUser
    public String create(UserFlow flow, RedirectAttributes redirectAttributes) {

        try {
            flowService.create(flow);
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("message", "遇到错误");
            return "redirect:/admin/userFlow/new";
        }
        return "redirect:/admin/userFlow/list";
    }

    /**
     * 删除一个党员流出信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @RequiresRoles(value = {RoleConstant.ROLE_ADMIN})
    public
    @ResponseBody
    Map<String, Object> delete(@PathVariable Long id) {
        Map<String, Object> map = new HashMap<>();
        flowService.delete(id);
        map.put("success", true);
        return map;
    }


    /**
     * 转到编辑党员流出页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    @RequiresUser
    public String edit(@PathVariable Long id, Model model) {
        UserFlow u = flowService.get(id);
        model.addAttribute("u", u);

        List<User> users = userService.findAllDy();
        model.addAttribute("dys", users);
        return "admin/user-flow/flow_update";
    }


    /**
     * 更新党员流出信息
     *
     * @param id
     * @param flow
     * @param redirectAttributes
     * @return
     */
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @RequiresRoles(value = {RoleConstant.ROLE_ADMIN})
    public String update(@PathVariable("id") long id, UserFlow flow, RedirectAttributes redirectAttributes) {

        try {
            flow.setId(id);
            flowService.update(flow);
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("message", "出现错误");
            return "redirect:/admin/userFlow/edit/" + id;
        }
        return "redirect:/admin/userFlow/list";
    }

}
